package com.cmcc.aregister.ui.adapter;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.model.AreaModel;
import com.cmcc.aregister.cs.model.Department;
import com.cmcc.aregister.cs.model.Doctor;
import com.cmcc.aregister.cs.model.Hospital;
import com.cmcc.aregister.cs.model.Schedule;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AppointmentAdapter extends BaseAdapter {
	private Context context;
	private String isHaoyuanTip;
	private int[] icon = { R.drawable.city_icon, R.drawable.hospital_icon,
			R.drawable.subject_icon, R.drawable.doctor_icon,
			R.drawable.time_icon, R.drawable.doctor_icon };
	private int[] icon_haoyuan_tip = { R.drawable.hospital_icon,
			R.drawable.subject_icon, R.drawable.doctor_icon,
			R.drawable.city_icon };
	private int[] text_default = { R.string.p_choice_city,
			R.string.p_choice_hospital, R.string.p_choice_subject,
			R.string.p_choice_doctor, R.string.p_choice_time };
	private int[] text_name = { R.string.city, R.string.hospital,
			R.string.subject, R.string.doctor, R.string.p_choice_time };
	private int[] text_name_haoyuan_tip = { R.string.hospital,
			R.string.subject, R.string.doctor, R.string.p_choice_tipType };
	private int[] text_default_haoyuan_tip = { R.string.p_choice_hospital,
			R.string.p_choice_subject, R.string.p_choice_doctor,
			R.string.p_choice_tipType };
//	private int[] background = { R.drawable.app_list_top_bg,
//			R.drawable.app_list_center_bg, R.drawable.app_list_last_bg };
	public int visibility = -1;
	public AreaModel city;
	public Hospital hospital;
	public Department department;
	public Doctor doctor;
	public Schedule schedule;
	public String[] content = { "", "", "", "", "" };
	public int size;

	public AppointmentAdapter(Context context, int size, String isHaoyuanTip) {
		this.context = context;
		this.size = size;
		this.isHaoyuanTip = isHaoyuanTip;
	}

	public int getCount() {
		return size;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.a_appointment, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon);
			holder.text_default = (TextView) convertView
					.findViewById(R.id.text_default);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.tag = (ImageView) convertView.findViewById(R.id.tag);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (size < 5) {
			if ("yes".equals(isHaoyuanTip)) {
				holder.icon.setBackgroundResource(icon_haoyuan_tip[position]);
			} else {
				holder.icon.setBackgroundResource(icon[position + 1]);
			}
		} else {
			holder.icon.setBackgroundResource(icon[position]);
		}

		if (position <= visibility) {// 显示前面几行，本地visibility最多为2，异地为3（比本地多了城市栏目）
			holder.text_default.setVisibility(View.GONE);
			if (size < 5) {
				if ("yes".equals(isHaoyuanTip)) {
					holder.name.setText(text_name_haoyuan_tip[position]);
				} else {
					holder.name.setText(text_name[position + 1]);
				}
			} else {
				holder.name.setText(text_name[position]);
			}
			holder.content.setText(content[position]);
			holder.name.setVisibility(View.VISIBLE);
			holder.content.setVisibility(View.VISIBLE);
		} else {// 表示最后一行，显示的是时间
			holder.text_default.setVisibility(View.VISIBLE);
			if (size < 5) {
				if ("yes".equals(isHaoyuanTip)) {
					holder.text_default
							.setText(text_default_haoyuan_tip[position]);
				} else {
					holder.text_default.setText(text_default[position + 1]);
				}
			} else {
				holder.text_default.setText(text_default[position]);
			}
			holder.name.setVisibility(View.GONE);
			holder.content.setVisibility(View.GONE);
		}
		convertView.setBackgroundResource(R.drawable.app_list_bg);
//		if (position == 0) {
//			convertView.setBackgroundResource(background[0]);
//		} else if (position == size - 1) {
//			convertView.setBackgroundResource(background[2]);
//		} else {
//			convertView.setBackgroundResource(background[1]);
//		}

		return convertView;
	}

	static class ViewHolder {
		ImageView icon;
		TextView text_default;
		TextView name;
		TextView content;
		ImageView tag;
	}
}